package com.camera.yl.widght;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CapatureButton extends View {

    private Paint outerCirclePaint;
    private Paint innerCirclePaint;
    private Paint iconPaint;
    private Path iconPath;
    private float radius;

    private OnCameraClickListener onCameraClickListener;

    public CapatureButton(Context context) {
        super(context);
        init();
    }

    public CapatureButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CapatureButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        outerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        outerCirclePaint.setColor(Color.WHITE);
        outerCirclePaint.setStyle(Paint.Style.STROKE);
        outerCirclePaint.setStrokeWidth(5);

        innerCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        innerCirclePaint.setColor(Color.TRANSPARENT);
        innerCirclePaint.setStyle(Paint.Style.FILL);

        iconPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        iconPaint.setColor(Color.WHITE);
        iconPaint.setStyle(Paint.Style.STROKE);
        iconPaint.setStrokeWidth(5);

        iconPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        radius = Math.min(w, h) / 2f - 10;
        createCameraIconPath();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;

        // Draw outer circle
        canvas.drawCircle(centerX, centerY, radius, outerCirclePaint);

        // Draw inner circle (transparent)
        canvas.drawCircle(centerX, centerY, radius - 5, innerCirclePaint);

        // Draw camera icon
        canvas.drawPath(iconPath, iconPaint);
    }

    private void createCameraIconPath() {
        float centerX = getWidth() / 2f;
        float centerY = getHeight() / 2f;
        float iconSize = radius * 0.6f;
        float halfIconSize = iconSize / 2f;
        float lensRadius = iconSize * 0.2f;

        iconPath.reset();

        // Draw the main camera body (rectangular)
        iconPath.addRect(centerX - halfIconSize, centerY - halfIconSize, centerX + halfIconSize, centerY + halfIconSize, Path.Direction.CW);

        // Draw the lens (circle)
        iconPath.addCircle(centerX, centerY, lensRadius, Path.Direction.CW);

        // Draw the top right small circle (viewfinder or flash)
        float smallCircleRadius = iconSize * 0.1f;
        iconPath.addCircle(centerX + halfIconSize - smallCircleRadius, centerY - halfIconSize + smallCircleRadius, smallCircleRadius, Path.Direction.CW);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (onCameraClickListener != null) {
                onCameraClickListener.onCameraClick();
            }
            return true;
        }
        return super.onTouchEvent(event);
    }

    // Interface for click events
    public interface OnCameraClickListener {
        void onCameraClick();
    }

    public void setOnCameraClickListener(OnCameraClickListener listener) {
        this.onCameraClickListener = listener;
    }
}


